﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using LotteryWCF.Constract;
using System.ServiceModel;
using System.ServiceModel.Channels;
namespace LotteryWCF.Commom
{
    public class Common
    {
        public static string GetOddsSql(int playTypeId, string IssueNo,int PlayTypeRadioID) 
        {
            List<OddsInfo> odds_lst = SqlHelper.getInstance.ExcuteForList<OddsInfo>(
                "select * from OddsInfo where playTypeId=" + playTypeId + " and IssueNo='" + IssueNo + "' and PlayTypeRadioID=" + PlayTypeRadioID);
            if(odds_lst.Count==0)
                return string.Empty;
            string sql = "select PlayTypeID ";
            foreach (var r in odds_lst)
            {
                sql += (",max(case Num when '"+r.Num+"' then NumOdds else 0 end) ["+r.Num+"]");
            }
            sql += " from OddsInfo where  playTypeId=" + playTypeId + " and IssueNo='" + IssueNo + "' and PlayTypeRadioID=" + PlayTypeRadioID + " group by PlayTypeID";
            return sql;
        }
        public static string GetRemainSql(int playTypeId, string IssueNo,int PlayTypeRadioID) 
        {
            List<OddsInfo> odds_lst = SqlHelper.getInstance.ExcuteForList<OddsInfo>(
                "select * from OddsInfo where playTypeId=" + playTypeId + " and IssueNo='" + IssueNo + "' and PlayTypeRadioID=" + PlayTypeRadioID);
            if (odds_lst.Count == 0)
                return string.Empty;
            string sql = "select PlayTypeID ";
            foreach (var r in odds_lst)
            {
                sql += (",max(case Num when '" + r.Num + "' then RemainderCount else 0 end) [" + r.Num + "]");
            }
            sql += " from OddsInfo where  playTypeId=" + playTypeId + " and IssueNo='" + IssueNo + "' and PlayTypeRadioID=" + PlayTypeRadioID + " group by PlayTypeID";
            return sql;
        }
        public static string GetOddsSql(int playTypeId, int PlayTypeRadioID)
        {
            List<OddsInfo> odds_lst = SqlHelper.getInstance.ExcuteForList<OddsInfo>(
                "select * from OddsInfo where playTypeId=" + playTypeId + "  and PlayTypeRadioID=" + PlayTypeRadioID);
            if (odds_lst.Count == 0)
                return string.Empty;
            string sql = "select PlayTypeID ";
            foreach (var r in odds_lst)
            {
                sql += (",max(case Num when '" + r.Num + "' then NumOdds else 0 end) [" + r.Num + "]");
            }
            sql += " from OddsInfo where  playTypeId=" + playTypeId + " and PlayTypeRadioID=" + PlayTypeRadioID + " group by PlayTypeID";
            return sql;
        }
        public static string GetRemainSql(int playTypeId, int PlayTypeRadioID)
        {
            List<OddsInfo> odds_lst = SqlHelper.getInstance.ExcuteForList<OddsInfo>(
                "select * from OddsInfo where playTypeId=" + playTypeId + " and PlayTypeRadioID=" + PlayTypeRadioID);
            if (odds_lst.Count == 0)
                return string.Empty;
            string sql = "select PlayTypeID ";
            foreach (var r in odds_lst)
            {
                sql += (",max(case Num when '" + r.Num + "' then RemainderCount else 0 end) [" + r.Num + "]");
            }
            sql += " from OddsInfo where  playTypeId=" + playTypeId + " and PlayTypeRadioID=" + PlayTypeRadioID + " group by PlayTypeID";
            return sql;
        }
        public static string GetClientIp()
        {
            OperationContext operationContext = OperationContext.Current;
            MessageProperties messageProperties = operationContext.IncomingMessageProperties;
            RemoteEndpointMessageProperty remoteEndpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            return remoteEndpointProperty.Address + ":" + remoteEndpointProperty.Port;
        }
    }
   
}